package com.market.goods.producer.impl;


import com.market.goods.producer.RocketProducer;
import com.market.goods.producer.dto.OrderPaidEvent;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Slf4j
@Service
public class RocketProducerImpl implements RocketProducer {

    @Resource
    private RocketMQTemplate rocketMQTemplate;

    @Override
    public void asyncSend(String topic, String message) {


        //send messgae asynchronously
        rocketMQTemplate.asyncSend(topic, message, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
//                System.out.printf("async onSucess SendResult=%s %n", sendResult);
                log.info("asyncSendOnSuccessSendResult={}",sendResult);
            }

            @Override
            public void onException(Throwable throwable) {
//                System.out.printf("async onException Throwable=%s %n", var1);
                log.info("asyncSendOnException={}",throwable);
            }

        });

    }

    @Override
    public void convertAndSend(String topic, String message) {
        //send message synchronously
        rocketMQTemplate.convertAndSend(topic, message);
    }

    @Override
    public void send(String topic, String message) {
        //send spring message
        rocketMQTemplate.send(topic, MessageBuilder.withPayload(message).build());
    }

    @Override
    public void syncSendOrderly(String topic, String message, String hashKey) {
        //Send messages orderly
        rocketMQTemplate.syncSendOrderly(topic,MessageBuilder.withPayload(message).build(),hashKey);
    }

    @Override
    public void sendMessageInTransaction(String destination, Message<?> message, Object object) {
//        rocketMQTemplate.sendMessageInTransaction(destination, message, object);
    }
}
